package com.zhou.service.impl;

import com.zhou.dao.UserDao;
import com.zhou.pojo.User;
import com.zhou.service.UserService;
import org.beetl.sql.core.SQLManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private SQLManager sqlManager;
    @Autowired
    private UserDao userDao;

    public Map getUser() {
        // 根据主键查找实体操作，可调用 unique 方法：
        Integer pk = 1;
        User unique = sqlManager.unique(User.class, pk);


        // 也可以调用 single 方法，如果未查找到，仅仅返回 null:
        User single = sqlManager.single(User.class, pk);
        HashMap<String, User> map = new HashMap<>();
        map.put("unique", unique);
        map.put("single", single);

        // 创建实体
        User user = User.builder().departmentId(1).createTime(new Date()).name("测试用户").build();
        sqlManager.insert(user);

        // 根据主键更新实体
        User user1 = User.builder().id(1).name("新名字").build();
        sqlManager.updateTemplateById(user1);
        return map;
    }


    public void testApi() {
        List<User> userList = userDao.selectSample("新名字");
        System.out.println("userList = " + userList);
    }
}
